package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.Announcement;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ck
 * @date 2021/11/16 15:56
 */

@Service
public class MessageService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendApi friendApi;

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private MovementApi movementApi;

    @DubboReference
    private AnnouncementApi announcementApi;
//    根据环信用户id，查询用户详情
    public UserInfoVo findUserInfoByHuanXin(String huanxinId) {
        //1、根据环信id查询用户
        User user = userApi.findByHuanXin(huanxinId);
        //2、根据用户id查询用户详情
        Long id = user.getId();
        UserInfo userInfo = userInfoApi.findById(id);
        //3、构建vo返回值
        UserInfoVo vo = new UserInfoVo();

        BeanUtils.copyProperties(userInfo,vo);

        if (userInfo.getAge() != null){
            vo.setAge(userInfo.getAge().toString());
        }

        return vo;
    }

    //添加好友关系
    public void contacts(Long friendId) {
        Long userId = UserHolder.getUserId();
        //1、将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + userId, Constants.HX_USER_PREFIX + friendId);
        if (!aBoolean){
            throw new BusinessException(ErrorResult.error());
        }
        //2、如果注册成功，记录好友关系到mongodb
        friendApi.save(userId,friendId);
    }

    public PageResult findUserList(Integer page, Integer pagesize, String keyword) {
        //1、调用API查询当前用户的好友数据 -- List<Friend>
        List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(),page,pagesize);
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        //2、提取数据列表中的好友id
        List<Long> userIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        //3、调用UserInfoAPI查询好友的用户详情
        UserInfo info = new UserInfo();
        info.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, info);
        //4、构造VO对象
        List<ContactVo> vos = new ArrayList<>();
        for (Friend friend : list) {
            UserInfo userInfo = map.get(friend.getFriendId());
            if(userInfo != null) {
                ContactVo vo = ContactVo.init(userInfo);
                vos.add(vo);
            }
        }
        return new PageResult(page,pagesize,0l,vos);
    }

    //查看喜欢列表
    public PageResult likesList(Integer page, Integer pagesize) {
        //1、获取当前用户id
        Long userId = UserHolder.getUserId();

        List<Comment> comments = commentApi.findList(userId);

        List<CommentVo> list = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            CommentVo commentVo = CommentVo.init(userInfo, comment);
            list.add(commentVo);
        }

        return new PageResult(page,pagesize,0l,list);
    }

    public PageResult lovesList(Integer page, Integer pagesize) {
        //1、获取当前用户id
        Long userId = UserHolder.getUserId();

        List<Comment> comments = commentApi.findLoveList(userId);

        List<CommentVo> list = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            CommentVo commentVo = CommentVo.init(userInfo, comment);
            list.add(commentVo);
        }
        return new PageResult(page,pagesize,0l,list);
    }

    public PageResult commentsList(Integer page, Integer pagesize) {
        //1、获取当前用户id
        Long userId = UserHolder.getUserId();

        List<Comment> comments = commentApi.findCommentsList(userId);

        List<CommentVo> list = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            CommentVo vo = CommentVo.init(userInfo, comment);
            list.add(vo);
        }
        return new PageResult(page,pagesize,0l,list);
    }

    public PageResult announcementsList(Integer page, Integer pagesize) {
        List<Announcement> list = announcementApi.findAnnouncementList();
        List<AnnouncementVo> vos= new ArrayList<>();
        for (Announcement announcement : list) {
            AnnouncementVo vo = AnnouncementVo.init(announcement);
            vos.add(vo);
        }
        return new PageResult(page,pagesize,0l,vos);
    }

    public void remove(Long likeUserId) {
        Long userId = UserHolder.getUserId();
        //1、将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + userId, Constants.HX_USER_PREFIX + likeUserId);
        if (!aBoolean){
            throw new BusinessException(ErrorResult.error());
        }
        //2、如果注册成功，记录好友关系到mongodb
        friendApi.remove(userId,likeUserId);
    }
}
